library(rClr)

mem <- function () {clrCallStatic('Rclr.TestCases', 'GetPrivateMemoryMegabtyes')}
sinkobj <- function(a) {clrCallStatic('Rclr.TestCases', 'SinkLargeObject', a)}
callGcMethname <- "CallGC"
cTypename <- "Rclr.TestCases"
forceDotNetGc <- function() { clrCallStatic(cTypename, callGcMethname) }

mem()


memMeasure <- function( createDataFunc, n, tag="") {
  gc()
  forceDotNetGc()
  res <- data.frame( when='before', mem=mem(), tag=tag)
  a <- createDataFunc(n)
  sinkobj(a)
  rm(a)
  gc()
  forceDotNetGc()
  res <- rbind(res, data.frame( when='after', mem=mem(), tag=tag))
  res
}

# numeric vectors
n = 2.5e7
memfp <- memMeasure(runif, n, tag='num')
for (i in 1:5) {
  memfp <- rbind( memfp, memMeasure(runif, n, tag='num'))
}

# integer vectors
n = 2.5e7
for (i in 1:5) {
  memfp <- rbind( memfp, memMeasure(function(n) {rep(1L,n)}, n, tag='int'))
}

# character vectors
n = 1e6
# memfp <- memMeasure(function(n) {rep('a',n)}, n, tag='char')
for (i in 1:5) {
  memfp <- rbind( memfp, memMeasure(function(n) {rep('a',n)}, n, tag='char'))
}
